using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using LightCAD.Core;
using LightCAD.Runtime;
using System;
using System.Reflection;

namespace LightCAD.Runtime;

public partial class CommandBar : UserControl
{
    public static int MaxHistoryLength = 1024 * 512;

    public CommandBar()
    {
        InitializeComponent();
        this.Initialized += CommandBar_Initialized;
    }


    private void CommandBar_Initialized(object? sender, EventArgs e)
    {

    }

    public LcDocument Document { get; internal set; }
    public string LastCommand { get; set; }

    public void ExecuteCommand(string command)
    {
        if ((command == "") && (this.LastCommand != null))
        {
            command = this.LastCommand;
        }
        this.ClearUsertext();
        var com = CommandCenter.Instance.FindCommand(command);
        var execCom = ((com != null) && (com.MainCommand != "")) ? com.MainCommand : command;
        if (CommandCenter.Instance.OnCommandExecute(this,execCom, com != null))
        {
            this.LastCommand = command;
        }
        else if (com != null)
        {
            this.ExecuteCommand(com);
        }
        else
        {
            AppendHistory("\r\n Unknown Command!!! " + command);
        }
    }

    public void SelectDocument(LcDocument document)
    {
        if (this.Document != null)
        {
            //this.Document.OnPrompt -= new vdDocument.PromptEventHandler(this.CommandAction_OnPrompt);
            //this.Document.ActionFinish -= new vdDocument.ActionFinishEventHandler(this.mDocument_ActionFinish);
        }
        this.Document = document;
        this.ClearUsertext();
        if (this.Document != null)
        {
            //this.Document.OnPrompt += new vdDocument.PromptEventHandler(this.CommandAction_OnPrompt);
            //this.Document.ActionFinish += new vdDocument.ActionFinishEventHandler(this.mDocument_ActionFinish);
            //this.Document.CommandAction.Prompt("Command:");
        }
    }



    private void ExecuteCommand(LcCommand com)
    {
        var method = CommandCenter.Instance.FindCommandMethod(com);
        if (method != null)
        {
            try
            {
                this.LastCommand = com.MainCommand;
                AppendHistory("\r\n" + this.PromptText.Text + " " + com.MainCommand);
                if (this.Document != null)
                {
                    method.Invoke(null, new object[] { this.Document });
                }
            }
            catch (Exception exception)
            {
                AppendHistory(exception.Message);
            }
            return;
        }
        AppendHistory("Could Not Load Command From Dll");
    }

    public void ExecuteCommandEx(string fullCommandLine)
    {
        var com = CommandCenter.Instance.ParseCommand(fullCommandLine); 
        if (com!=null)
        {
            this.ExecuteCommand(com);
        }
    }


    private void ClearUsertext()
    {
        this.InputText.Text = "";
    }
    private void AppendHistory(string text)
    {
        this.HistoryText.Text += text;
    }
   
}